#coding=utf-8
from threading import Timer
import numpy as np
import cv2
import time

i = 0
time_interval = 1.0
lower_blue = np.array([0,0,0])
upper_blue = np.array([170,255,46])
cap = cv2.VideoCapture(0)

def frame_rate():
	global i
	x = i
	print x
	i = 0
	t = Timer(time_interval,frame_rate)
	t.start()

t = Timer(time_interval,frame_rate)
t.start()


while(True):
	global i
	i = i + 1
	ret, frame = cap.read()
	gray   = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
	gray1  = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
	# cv2.fastNlMeansDenoisingMulti(gray,10,7,21)
	ret,thresh=cv2.threshold(gray,127,255,cv2.THRESH_BINARY)
	contours, hierarchy = cv2.findContours(
										thresh,
										cv2.RETR_TREE,
										cv2.CHAIN_APPROX_NONE)
	#寻找轮廓
	# print "len(contours):",len(contours)
	F = []
	for j in range(len(contours)):
		if cv2.contourArea(contours[j]) > 1500:
			# print "Area",cv2.contourArea(contours[i])
			# print "Length",cv2.arcLength(contours[i],True)
			F.append(contours[j])
	cv2.drawContours(gray, F, -1, (0,255,0), 3)
	cv2.imshow("gray1",gray )
	if cv2.waitKey(1) & 0xFF == ord('q'):
		break
cap.release()
cv2.destroyAllWindows()
